#!/usr/bin/node

const log = console.log;
const Router = require('koa-router'),
      users = require('../models/users.js'),
      {JWT_KEY} = require('../lib/constant.js'),
      jwt = require('jsonwebtoken'),
      _ = require('lodash');

let router = new Router();

router.prefix('/api/users');

router.post('/login',async (ctx,next)=>{
  let {email, password} = ctx.request.body;

  let res =await users.login(email,password);

  if(res.code === 0){
    res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
  }

  ctx.body = res; 
});

module.exports = router;

log('use routes/users.js');

